package com.manulife.hk.performance.services;

import com.manulife.hk.performance.domains.SalesMapEntity;
import com.manulife.hk.performance.repository.SalesMapRepository;

import org.apache.commons.collections.IteratorUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class SalesMapServiceImpl implements SalesMapService {

	private final Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private SalesMapRepository salesMapRepository;

	@SuppressWarnings("unchecked")
	@Override
	public List<SalesMapEntity> addList(List<SalesMapEntity> issueSales) {

		Iterable<SalesMapEntity> savedList = salesMapRepository.save(issueSales);
		return IteratorUtils.toList(savedList.iterator());
	}

	@Override
	public void remove(Long id) {
		SalesMapEntity entity = salesMapRepository.findOne(id);
		if (null == entity) {
			throw new EntityNotFoundException("SalesMap record not found: id=" + id);
		}
		log.info("SalesMap record to be deleted: {}", entity.toString());
		salesMapRepository.delete(id);
	}

	@Override
	public List<SalesMapEntity> findAll(SalesMapEntity submitIssueSales) {
		return salesMapRepository.findAll(new Specification<SalesMapEntity>() {
			@Override
			public Predicate toPredicate(Root<SalesMapEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = getQueryCondition(submitIssueSales, root, cb);
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}
		});
	}

	private List<Predicate> getQueryCondition(SalesMapEntity submitIssueSales, Root<SalesMapEntity> root,
			CriteriaBuilder cb) {

		List<Predicate> list = new ArrayList<>();
		String submitIssueSalesMapKey = submitIssueSales.getSubmitIssueSalesMapKey();
		String submitSalesKey = submitIssueSales.getSubmitSalesKey();
		String submitSalesSubKey = submitIssueSales.getSubmitSalesSubKey();
		String issueSalesKey = submitIssueSales.getIssueSalesKey();

		if (StringUtils.isNotBlank(submitIssueSalesMapKey)) {
			list.add(cb.equal(root.get("submitIssueSalesMapKey").as(String.class), submitIssueSalesMapKey));
		}
		if (StringUtils.isNotBlank(submitSalesKey)) {
			list.add(cb.equal(root.get("submitSalesKey").as(String.class), submitSalesKey));
		}
		if (StringUtils.isNotBlank(submitSalesSubKey)) {
			list.add(cb.equal(root.get("submitSalesSubKey").as(String.class), submitSalesSubKey));
		}
		if (StringUtils.isNotBlank(issueSalesKey)) {
			list.add(cb.equal(root.get("issueSalesKey").as(String.class), issueSalesKey));
		}
		return list;
	}

}
